import { createRouter, createWebHashHistory } from "vue-router";
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: "/login",
    name: "login",
    component: () =>
      import("../views/LoginView.vue"),
  },
  {
    path: "/top",
    name: "top",
    redirect: '/home',
    component:()=>import("../components/TopView.vue"),
    children:[
    {
      path: '/home',
      name: 'home',
      component:()=>import("../views/HomeView.vue"),
    },
    {
      path: '/users',
      name: 'users',
      component:()=>import("../views/mms/UsersVIew.vue"),
    },
    {
      path: '/pets',
      name: 'pets',
      component:()=>import("../views/mms/PetsView.vue"),
    },
    {
      path: '/carousel',
      name: 'carousel',
      component:()=>import("../views/mms/Carousel.vue"),
    },
    {
      path: '/photos',
      name: 'photos',
      component:()=>import("../views/serve/PhotoView.vue"),
    },
    {
      path: '/nurse',
      name: 'nurse',
      component:()=>import("../views/serve/NurseView.vue"),
    },
    {
      path: '/foster',
      name: 'foster',
      component:()=>import("../views/serve/FosterView.vue"),
    },
    {
      path: '/health',
      name: 'health',
      component:()=>import("../views/mms/HealthView.vue"),
    },
    {
      path: '/remark',
      name: 'remark',
      component:()=>import("../views/forum/RemarkView"),
    },
    {
      path: '/article',
      name: 'article',
      component:()=>import("../views/forum/ArticleView"),
    }
    ]
  },
  {
    path: "/chat",
    name: "chat",
    redirect: '/main',
    component:()=>import("../views/chat/ChatTopView.vue"),
    children:[
      {
        path: '/main',
        name: 'main',
        component:()=>import("../views/chat/MainView.vue"),
      },
      {
        path: '/persion',
        name: 'persion',
        component:()=>import("../views/chat/Persion.vue"),
      },
      {
        path: '/sample',
        name: 'sample',
        component:()=>import("../views/chat/SampleView.vue"),
      },
      {
        path: '/photo',
        name: 'photo',
        component:()=>import("../views/chat/PhotoView.vue"),
      },
      {
        path: '/cos',
        name: 'cos',
        component:()=>import("../views/chat/CosView.vue"),
      },
      {
        path: '/articledetails',
        name: 'articledetails',
        component:()=>import("../views/chat/Articledetails.vue"),
      },
    ]
  }

];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to,from,next)=>{
  console.log(to.path);
  if(to.path == '/login'){
    return next();
  }
  const tokenStr = window.sessionStorage.getItem("token");
  if(!tokenStr){
    //没有凭证跳回login
    return next('/login');
  }
  next();
});

export default router;
